package com.servlet.article;

import com.entity.Code;
import com.service.ArticleService;
import com.utils.JsonUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 查询页码，根据数据的数量返回页码。offset是每页显示多少条文章
 * 必选参数： offset
 */
@WebServlet(name = "pageNum", value = "/pageNum")
public class pageNum extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        ArticleService articleService = new ArticleService();
        int offset = Integer.parseInt(request.getParameter("offset"));
        int count = articleService.count();
        int page = count / offset +1;
        System.out.println("共有"+page+"页");
        Code code = new Code();
        code.setMsg("共有"+page+"页，"+count+"条数据");
        code.setCode("200");
        code.setData(page);
        String s = jsonUtil.JavaToJson(code);
        response.getWriter().print(s);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
